import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.image.codec.jpeg.*;

/** Class CapturingCanvas3D, using the instructions from the Java3D 
 FAQ pages on how to capture a still image in jpeg format.
 
 A capture button would call a method that looks like
 
 
 public static void captureImage(CapturingCanvas3D MyCanvas3D) {
 MyCanvas3D.writeJPEG_ = true;
 MyCanvas3D.repaint();
 }
 
 
 Peter Z. Kunszt
 Johns Hopkins University
 Dept of Physics and Astronomy
 Baltimore MD
 */
public class CapturingCanvas3D extends Canvas3D  {
	
	private static final long serialVersionUID = 4283479329160417927L;
	public boolean writeJPEG_;
	private int postSwapCount_;
	public String outputName_;
	
	public CapturingCanvas3D(GraphicsConfiguration gc) {
		super(gc);
		postSwapCount_ = 0;
	}
	
	public void postSwap() {
		if(writeJPEG_) {
			GraphicsContext3D  ctx = getGraphicsContext3D();
			// The raster components need all be set!
			Raster ras = new Raster(	
					new Point3f(-1.0f,-1.0f,-1.0f), 
					Raster.RASTER_COLOR,
					0, 0, 640, 640,
					new ImageComponent2D(	ImageComponent.FORMAT_RGB,
							new BufferedImage(640, 
									640, 
									BufferedImage.TYPE_INT_RGB)),
									null);
			
			ctx.readRaster(ras);
			
			// Now strip out the image info
			BufferedImage img = ras.getImage().getImage();
			
			String fileName;
			if (outputName_.equals(null)) {
				fileName = "Capture"+postSwapCount_+".jpg";
			}
			else {
				fileName = outputName_+".jpg";
			}
			
			// write that to disk....
			try {
				FileOutputStream out = new FileOutputStream(fileName);
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
				JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
				param.setQuality(0.9f,false); // 90% qualith JPEG
				encoder.setJPEGEncodeParam(param);
				encoder.encode(img);
				writeJPEG_ = false;
				out.close();
			} catch ( IOException e ) {
				System.out.println("I/O exception!");
			}
			postSwapCount_++;
			System.out.println("Writing JPEG : "+fileName);
		}
	}
}
